package com.yugao.fintech.libra.shortlink.controller;

import com.yugao.fintech.libra.common.ram.ActionType;
import com.yugao.fintech.libra.common.ram.RamResource;
import com.yugao.fintech.libra.shortlink.model.entity.LinkDomain;
import com.yugao.fintech.libra.shortlink.model.rqrs.DomainListReq;
import com.yugao.fintech.libra.shortlink.model.rqrs.DomainReq;
import com.yugao.fintech.libra.shortlink.service.DomainService;
import com.yugao.fintech.meheco.core.domain.ResultVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Collections;
import java.util.List;

@RestController
@RequestMapping("/linkDomain")
@Tag(name = "短链域名")
public class DomainController {
    @Autowired
    private DomainService domainService;

    @Operation(description = "保存-域名")
    @PostMapping("/saveDomain")
    @RamResource(action = "sl:linkDomain:save", name = "保存短链域名", type = ActionType.WRITE)
    public ResultVO<?> saveDomain(@Validated @RequestBody DomainReq rq) {
        return ResultVO.success(domainService.saveDomain(rq));
    }

    @Operation(description = "更新-域名")
    @PostMapping("/updateDomain")
    @RamResource(action = "sl:linkDomain:update", name = "更新短链域名", type = ActionType.WRITE)
    public ResultVO<?> updateDomain(@Validated(UpdateGroup.class) @RequestBody DomainReq rq) {
        return ResultVO.success(domainService.updateDomain(rq));
    }

    @Operation(description = "批量删除-域名")
    @RamResource(action = "sl:linkDomain:delete", name = "删除短链域名", type = ActionType.WRITE)
    @DeleteMapping("/batchDeleteDomain")
    public ResultVO<?> batchDeleteDomain(@RequestParam(value = "ids") List<Long> ids) {
        return ResultVO.success(domainService.deleteDomainByIds(ids));
    }

    @Operation(description = "删除-域名")
    @RamResource(action = "sl:linkDomain:delete", name = "删除短链域名", type = ActionType.WRITE)
    @DeleteMapping("/deleteDomain")
    public ResultVO<?> deleteDomain(@RequestParam(value = "id") Long id) {
        return ResultVO.success(domainService.deleteDomainByIds(Collections.singletonList(id)));
    }

    @Operation(description = "查询列表-域名")
    @RamResource(action = "sl:linkDomain:query", name = "查询短链域名", type = ActionType.READ)
    @GetMapping("/listDomain")
    public ResultVO<List<LinkDomain>> listDomain(DomainListReq query) {
        return ResultVO.success(domainService.listDomain(query));
    }
}
